<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="上级租户编号" prop="parentCode">
        <el-input
          v-model="queryParams.parentCode"
          placeholder="请输入上级租户编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户名称" prop="tenantName">
        <el-input
          v-model="queryParams.tenantName"
          placeholder="请输入租户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="tenantNickname">
        <el-input
          v-model="queryParams.tenantNickname"
          placeholder="请输入用户昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入租户邮箱"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入联系地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="头图标文件名称" prop="titleName">
        <el-input
          v-model="queryParams.titleName"
          placeholder="请输入头图标文件名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="背景文件名称" prop="bgName">
        <el-input
          v-model="queryParams.bgName"
          placeholder="请输入背景文件名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="首页请求地址" prop="homePath">
        <el-input
          v-model="queryParams.homePath"
          placeholder="请输入首页请求地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择租户状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="有效期限" prop="tenantterm">
        <el-input
          v-model="queryParams.tenantterm"
          placeholder="请输入有效期限"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="激活码" prop="tenantkey">
        <el-input
          v-model="queryParams.tenantkey"
          placeholder="请输入激活码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户域名,多个以,分割 www.abc.com,www.def.com" prop="tenantdomain">
        <el-input
          v-model="queryParams.tenantdomain"
          placeholder="请输入租户域名,多个以,分割 www.abc.com,www.def.com"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:tenant:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:tenant:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:tenant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:tenant:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tenantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="租户编号" align="center" prop="tenantCode" />
      <el-table-column label="上级租户编号" align="center" prop="parentCode" />
      <el-table-column label="租户名称" align="center" prop="tenantName" />
      <el-table-column label="用户昵称" align="center" prop="tenantNickname" />
      <el-table-column label="租户邮箱" align="center" prop="email" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" />
      <el-table-column label="联系地址" align="center" prop="address" />
      <el-table-column label="头图标文件名称" align="center" prop="titleName" />
      <el-table-column label="头图标" align="center" prop="titlePath" />
      <el-table-column label="背景文件名称" align="center" prop="bgName" />
      <el-table-column label="背景图片" align="center" prop="bgPath" />
      <el-table-column label="首页请求地址" align="center" prop="homePath" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="租户状态" align="center" prop="status" />
      <el-table-column label="有效期限" align="center" prop="tenantterm" />
      <el-table-column label="激活码" align="center" prop="tenantkey" />
      <el-table-column label="租户域名,多个以,分割 www.abc.com,www.def.com" align="center" prop="tenantdomain" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:tenant:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:tenant:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
          <el-col :span="12">
            <el-form-item label="上级租户" prop="parentCode">
              <el-select v-model="form.parentCode" placeholder="请选择上级租户" clearable
                         :style="{width: '100%'}">
                <el-option v-for="(item, index) in parentCodeOptions" :key="index" :label="item.label"
                           :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="租户名称" prop="tenantName">
              <el-input v-model="form.tenantName" placeholder="请输入租户名称" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="租户昵称" prop="tenantNickname">
              <el-input v-model="form.tenantNickname" placeholder="请输入租户昵称" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input v-model="form.phonenumber" placeholder="请输入手机号码" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系地址" prop="address">
              <el-input v-model="form.address" type="textarea" placeholder="请输入联系地址"
                        :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效期限" prop="tenantTerm">
              <el-date-picker v-model="form.tenantTerm" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                              :style="{width: '100%'}" placeholder="请选择有效期限" clearable></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首页请求地址" prop="homePath">
              <el-input v-model="form.homePath" placeholder="请输入首页请求地址" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="激活码" prop="tenantKey">
              <el-input v-model="form.tenantkey" placeholder="请输入激活码" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="租户域名" prop="tenantDomain">
              <el-input v-model="form.tenantdomain" placeholder="请输入租户域名" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="删除标志" prop="delFlag">
              <el-switch v-model="form.delFlag"  :active-value="1" :inactive-value="0" disabled></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="租户状态" prop="status">
              <el-switch v-model="form.status" active-value="0" inactive-value="1"></el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="头图标名称" prop="titleName">
              <el-input v-model="form.titleName" placeholder="请输入头图标名称" clearable
                        :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="logo名称" prop="bgName">
              <el-input v-model="form.bgName" placeholder="请输入logo名称" clearable :style="{width: '100%'}">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="头图" prop="titlePath">
              <el-upload ref="titlePath" :multiple="false"
                         :headers="headers"
                         :file-list="titlePathfileList" :action="titlePathAction"
                         :on-success="onSuccess"
                         :before-upload="titlePathBeforeUpload" list-type="picture-card">
                <img :src="form.titlePath" class="el-upload el-upload--picture-card" />
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="logo图片" prop="bgPath">
              <el-upload ref="bgPath" :file-list="bgPathfileList" :action="bgPathAction"
                         :before-upload="bgPathBeforeUpload" list-type="picture-card">
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"
                        :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTenant, getTenant, delTenant, addTenant, updateTenant, exportTenant } from "@/api/system/tenant";

export default {
  name: "Tenant",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租户信息表格数据
      tenantList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentCode: undefined,
        tenantName: undefined,
        tenantNickname: undefined,
        email: undefined,
        phonenumber: undefined,
        address: undefined,
        titleName: undefined,
        titlePath: undefined,
        bgName: undefined,
        bgPath: undefined,
        homePath: undefined,
        status: undefined,
        tenantterm: undefined,
        tenantkey: undefined,
        tenantdomain: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentCode: [{
          required: true,
          message: '请选择上级租户',
          trigger: 'change'
        }],
        tenantName: [{
          required: true,
          message: '请输入租户名称',
          trigger: 'blur'
        }],
        tenantNickname: [{
          required: true,
          message: '请输入租户昵称',
          trigger: 'blur'
        }],
        email: [{
          pattern: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
          message: '输入正确邮箱格式',
          trigger: 'blur'
        }],
        phonenumber: [{
          required: true,
          message: '请输入手机号码',
          trigger: 'blur'
        }, {
          pattern: /^1[34578]\d{9}$/,
          message: '请输入正确手机号码',
          trigger: 'blur'
        }],
        address: [],
        tenantTerm: [],
        homePath: [],
        tenantKey: [],
        tenantDomain: [],
        titleName: [],
        bgName: [],
        remark: [],
      },
      titlePathAction: process.env.VUE_APP_BASE_API+ '/common/uploadFile2Cos',
      titlePathfileList: [],
      bgPathAction: process.env.VUE_APP_BASE_API+ '/common/uploadFile2Cos',
      bgPathfileList: [],
      parentCodeOptions: [{
        "label": "平台",
        "value": 0
      }],
      headers: window.CONFIG.headers,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询租户信息列表 */
    getList() {
      this.loading = true;
      listTenant(this.queryParams).then(response => {
        this.tenantList = response.rows;
        this.total = Number.parseInt(response.total);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tenantCode: undefined,
        parentCode: undefined,
        tenantName: undefined,
        tenantNickname: undefined,
        email: undefined,
        phonenumber: undefined,
        address: undefined,
        titleName: undefined,
        titlePath: undefined,
        bgName: undefined,
        bgPath: undefined,
        homePath: undefined,
        remark: undefined,
        status: "0",
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        tenantterm: undefined,
        tenantkey: undefined,
        tenantdomain: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.tenantCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const tenantCode = row.tenantCode || this.ids
      getTenant(tenantCode).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改租户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.debug(this.form);
          if (this.form.tenantCode != null) {
            updateTenant(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTenant(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tenantCodes = row.tenantCode || this.ids;
      this.$confirm('是否确认删除租户信息编号为"' + tenantCodes + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTenant(tenantCodes);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有租户信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTenant(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    titlePathBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2MB')
      }
      return isRightSize
    },
    bgPathBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 2
      if (!isRightSize) {
        this.$message.error('文件大小超过 2MB')
      }
      return isRightSize
    },
    onSuccess(res, file){
      if (res.code === 200) {
          this.$set(this.form,"titlePath",res.url);

      }
    }
  }
};
</script>
<style scoped>
/*  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }*/
</style>
